package com.longcai.controller.sys;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.sys.ClyjDict;
import com.longcai.service.sys.ClyjDictService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
public class ClyjDictController {
	
	@Resource
	private ClyjDictService dictionariesService;
	
	/**
	 * 查询数据字典
	 * @return
	 */
	@RequestMapping(value="/selectDictionaries")
	@ResponseBody
	public List<ClyjDict> Dictionaries(){
		QueryWrapper<ClyjDict> wrapper = new QueryWrapper<ClyjDict>();
		wrapper.orderByAsc("sort_no");
		List<ClyjDict> demandBasicInfos = dictionariesService.selectList(wrapper);
		return demandBasicInfos;
	}
	/**
	 * 插入数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/insertDictionaries")
	@ResponseBody
	public ApiResult insertDictionaries(@RequestBody JSONObject params){
		Map map = new HashMap();
		try {
			ClyjDict dictInfo = new ClyjDict();
			dictInfo.setAttcode( params.getString("attCode"));
			dictInfo.setAttname( params.getString("attName"));
			dictInfo.setCreateTime(LocalDateTime.now());
			dictInfo.setUpdateTime(LocalDateTime.now());
			dictInfo.setDitLevels( params.getString("level"));
			dictInfo.setId(UUID.randomUUID().toString());
			dictInfo.setPcode(params.getString("dictCode"));
			dictInfo.setSortNo( params.getString("sequence"));
			dictInfo.setRemarks(params.getString("remarks"));
			boolean insert = dictionariesService.insert(dictInfo);
			map.put("message", "新增成功");
			map.put("flag", "S");
			return ApiResult.success(map);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			map.put("message", "新增失败");
			return ApiResult.fail("字典添加失败！");
		}
	}
	/**
	 * 删除数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/deleteDictionaries")
	@ResponseBody
	public Map<String,Object> deleteDictionaries(@RequestBody JSONObject params){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			String att = params.getString("rowData");
			att = att.replace("[", "").replace("]", "").replace(" ", "");
			String[] attCode = att.split(",");
			for (String s : attCode) {
				boolean deleteById = dictionariesService.deleteById(s);
			}
			result.put("message", "删除成功");
			result.put("flag", "S");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			result.put("message", "删除失败");
			return result;
		}
	}
	/**
	 * 修改数据
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateDictionaries")
	@ResponseBody
	public Map<String,Object> updateDictionaries(@RequestBody JSONObject params){
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			ClyjDict dictInfo = new ClyjDict();
			dictInfo.setAttcode( params.getString("attCode"));
			dictInfo.setAttname( params.getString("attName"));
			dictInfo.setUpdateTime(LocalDateTime.now());
			dictInfo.setDitLevels( params.getString("level"));
			dictInfo.setId(params.getString("id"));
			dictInfo.setSortNo( params.getString("sequence"));
			dictInfo.setRemarks(params.getString("remarks"));
			boolean flag = dictionariesService.updateById(dictInfo);
			result.put("message", "修改成功");
			result.put("flag", "S");
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
			result.put("message", "修改失败");
			return result;
		}
	}
	/**
	 * 校验输入值
	 * @param dictInfo
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/compareValue")
	@ResponseBody
	public Map<String,Object> compareValue(@RequestBody JSONObject params){
		Map<String,Object> result = new HashMap<String,Object>();
		String attCode = params.getString("attCode");
		String dictCode = params.getString("dictCode");
		String id = params.getString("id");
		
		
		QueryWrapper<ClyjDict> dictInfo = new QueryWrapper<ClyjDict>();
		dictInfo.eq("attcode", attCode);
		if(dictCode!=null && dictCode!="")	 dictInfo.eq("pcode", dictCode);
		else	dictInfo.isNull("pcode");
		//判断是修改还是新增
		if(id!=null && id!="")			dictInfo.ne("id", id);
		List<ClyjDict> list = dictionariesService.selectList(dictInfo);
		if(list!=null && list.size()!=0){
			result.put("flag", "CODE");
			return result;
		}
		//修改
		
		return result;
	}
	/**
	 * 时间的校验
	 * @param demandInfo
	 * @return
	 */

}


